/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import model.PionType;
import model.Spel;
import model.Stuk;

/**
 *
 * @author Stijn Delbeke
 */
public class SpeelBord extends JPanel implements MouseListener {

    private final int VAKGROOTTE = 65;
    private Vak[][] vakken;
    private Spel game;
    private InfoBord infobord;
    Random rnd = new Random(System.currentTimeMillis());
    private Vak bron;
    private Vak doel;

    public SpeelBord(Spel game, InfoBord infobord) {
        this.game = game;
        this.infobord = infobord;

        setLayout(new GridLayout(10, 0));
        voegVakkenToe();

        promptSpelerOfPc();

        promptAutoOfManueel();

    }

    private void promptSpelerOfPc() {
        Object[] options = {"JA", "NEE"};
        JOptionPane optPane = new JOptionPane("Wilt u tegen de computer spelen?",
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.DEFAULT_OPTION,
                null, options, options[0]);
        JDialog dialog = optPane.createDialog(null, "Pionnen plaatsen");
        dialog.setVisible(true);
        String selectedValue = (String) optPane.getValue();

        if (selectedValue == null) {
            selectedValue = "JA";
        }
        else {
            if (selectedValue.equals("JA")) {
                game.setRivaalIsMens(false);
            }
            else {
                game.setRivaalIsMens(true);
            }

        }
    }

    private void promptAutoOfManueel() {
        Object[] options = {"AUTOMATISCH", "HANDMATIG"};
        JOptionPane optPane = new JOptionPane("Wilt u uw pionnen automatisch of handmatig plaatsen?",
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.DEFAULT_OPTION,
                null, options, options[0]);
        JDialog dialog = optPane.createDialog(null, "Pionnen plaatsen");
        dialog.setVisible(true);
        String selectedValue = (String) optPane.getValue();

        if (selectedValue == null) {
            selectedValue = "AUTOMATISCH";
        }

        if (selectedValue.equals("AUTOMATISCH")) {
            zetStukkenAutomatisch(game.BLAUW);
            zetStukkenAutomatisch(game.ROOD);

        }
        else {
            if (!game.getRivaalIsMens()) {
                zetStukkenAutomatisch(game.BLAUW);
            }
        }
    }

    private void zetStukkenAutomatisch(String leger) {

        int x, xMax;
        int y, yMax;

        xMax = 10;
        yMax = 4;

        for (Stuk s : game.getLeger(leger)) {
            s.setLeger(leger);

            if (leger.equals(game.BLAUW)) {
                x = rnd.nextInt(xMax);
                y = rnd.nextInt(yMax);
            }
            else {
                x = rnd.nextInt(xMax);
                y = 10 - yMax + rnd.nextInt(yMax);
            }

            while (vakken[y][x].bevatPion()) {
                if (leger.equals(game.BLAUW)) {
                    x = rnd.nextInt(xMax);
                    y = rnd.nextInt(yMax);
                }
                else {
                    x = rnd.nextInt(xMax);
                    y = 10 - yMax + rnd.nextInt(yMax);
                }
            }

            vakken[y][x].addMouseListener(this);
            s.setLeger(leger);
            vakken[y][x].setPion(s);
            if (leger.equals(game.BLAUW) && !(game.getRivaalIsMens())) {
                vakken[y][x].createIcon("../icons/" + "VERBORGEN" + "_" + "BLAUW" + ".jpg");
            }
            else {
                vakken[y][x].createIcon(s.getType(), leger);
            }
            infobord.verminderPion(s.getType(), leger);
            infobord.controleerPionnen();
        }



    }

    private void voegVakkenToe() {
        vakken = game.getVakken();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                vakken[i][j].addMouseListener(this);
                vakken[i][j].setPreferredSize(new Dimension(VAKGROOTTE, VAKGROOTTE));
                this.add(vakken[i][j]);
            }
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        Vak geklikt = (Vak) e.getSource();

        if (infobord.isAllesGezet()) {
            //Spel kan beginnen
            if (e.getButton() == 1) {
                if (geklikt.bevatPion() && !geklikt.getPion().getType().equals(PionType.BOM) && !geklikt.getPion().getType().equals(PionType.VLAG)) {
                    if (geklikt.getPion().getLeger().equals(game.getBeurt())) {
                        if (bron != null) {
                            bron.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                        }

                        bron = geklikt;
                        if (game.getBeurt().equals(game.ROOD)) {
                            bron.setBorder(BorderFactory.createLineBorder(Color.RED, 4));
                        }
                        else {
                            bron.setBorder(BorderFactory.createLineBorder(Color.BLUE, 4));
                        }
                    }
                }
            }
            else if (e.getButton() == 3) {
                if (bron != null && geklikt.isLand()) {
                    doel = geklikt;
                    if (game.isGeldigeZet(bron, doel)) {
                        if (!(geklikt.bevatPion())) {
                            game.verplaatsPion(bron, doel);
                            game.wijzigSpeler();
                            bron.repaint();
                            doel.repaint();
                            bron.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                            bron = null;
                            doel = null;
                        }
                        else if (geklikt.bevatPion()) {
                            if (!(doel.getPion().getLeger().equals(bron.getPion().getLeger()))) {

                                game.battle(bron, doel);
                                game.wijzigSpeler();
                                bron.repaint();
                                doel.repaint();
                                bron.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                                bron = null;
                                doel = null;

                            }
                        }

                    }
                    else {
                        bron.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                        bron = null;
                        doel = null;
                    }
                }

            }
        }
        else {
            //Bord nog in opbouw
            if (e.getButton() == 3) {
                if (infobord.getGekozenVak() != null && geklikt.isLand()) {
                    doel = geklikt;
                    if (!doel.bevatPion() && geldigGebied(infobord.getGekozenVak().getPion().getLeger(), doel.getyPos())) {
                        doel.setPion(infobord.getGekozenVak().getPion());
                        doel.createIcon(infobord.getGekozenVak().getPion().getType(), infobord.getGekozenVak().getPion().getLeger());
                        infobord.verminderPion(infobord.getGekozenVak().getPion().getType(), infobord.getGekozenVak().getPion().getLeger());
                        infobord.controleerPionnen();
                        infobord.getGekozenVak().setBackground(Color.ORANGE);
                        infobord.resetGekozenVak();
                    }
                }
            }
        }

    }

    boolean geldigGebied(String leger, int y) {
        return leger.equals(game.BLAUW) ? y < 4 : y > 5;
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}
